import sys

# decorators (must be first)
def demo(f=None,demos=[]): 
  if f: demos.append(f); return f
  for d in demos: 
    print '\n--|',d.func_name,'|','-'*40,'\n'; d()

def test(f=None,tests=[]): 
  if f: tests.append(f); return f
  passes=fails=0
  for t in tests: 
    want,got = t()
    if want == got:
      passes += 1; print "PASSED:",t.func_name
    else:
      fails += 1;  print "FAILED",t.func_name
  if tests:
    print round(100*pased/(passes+failes)), "% PASSED"

def demoed(cxt,globs,args=sys.argv):
  if cxt == "__main__": eval(args[1] +'()',globs) if len(args)==2 else demo()

def tested(cxt,globs,args=sys.argv):
  if cxt == "__main__": eval(args[1] +'()') if len(args)==2 else demo()

####################
class Deep(dict) :
  def __getitem__(self,x) :
    if x in self: return self.get(x)
    new = self[x] = Deep()
    return new
  def push(self,k,v) :
    all = self[k] if k in self else []
    all.append(v)
    self[k]=all
    return all
  def at(self,lst,default=None) :
    here=self
    for key in lst:
      if not key in here:
        return default
      here = here[key]
    return here
  def inc(self,k,n=1):
    new = (self[k] if k in self else 0) + n
    self[k] = new
    return new 

@demo
def _deeped() :
  d=Deep()
  d[1][2][3] = 22
  d[1][2][4] = 44
  print d

def pp(x): pprint.pprint(vars(x))

def strs(*dicts) :
   out={}
   for d in dicts: 
     for key in d: out[key]=d[key]
   show = ""
   for key,val in out.items():
     show = show + ':'+str(key)+' '+str(val)+' '
   return show

def rows(file, n=0, bad=r'["\' \t\r\n]',sep=',') :
  for line in open(file,'r') :
    n += 1
    line = re.sub(bad,"",line).split(sep)
    if line: yield n,line
